package com.ruoyi.sbxm.jtlxpw.controller;

import java.util.List;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.common.utils.uuid.IdUtils;
import com.ruoyi.sbxm.jtlxpw.domain.JtlxpwFj;
import com.ruoyi.sbxm.jtlxpw.service.IJtlxpwService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;

import java.io.InputStream;

import org.springframework.web.multipart.MultipartFile;
import com.ruoyi.sbxm.jtlxpw.domain.Jtlxpw;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 集团立项批文Controller
 *
 * @author huacai
 * @date 2024-11-11
 */
@RestController
@RequestMapping("/sbxm/jtlxpw")
public class JtlxpwController extends BaseController {
    @Resource
    private IJtlxpwService jtlxpwService;

    /**
     * 查询集团立项批文列表
     */
    @PreAuthorize("@ss.hasPermi('sbxm:jtlxpw:list')")
    @GetMapping("/list")
    public TableDataInfo list(Jtlxpw jtlxpw) {
        startPage();
        List<Jtlxpw> list = jtlxpwService.selectJtlxpwList(jtlxpw);
        return getDataTable(list);
    }

    /**
     * 导出集团立项批文列表
     */
    @PreAuthorize("@ss.hasPermi('sbxm:jtlxpw:export')")
    @Log(title = "集团立项批文", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, Jtlxpw jtlxpw) {
        List<Jtlxpw> list = jtlxpwService.selectJtlxpwList(jtlxpw);
        ExcelUtil<Jtlxpw> util = new ExcelUtil<Jtlxpw>(Jtlxpw.class);
        util.exportExcel(response, list, "集团立项批文数据");
    }

    /**
     * 下载模板
     */
    @PostMapping("/importTemplate")
    public void importTemplate(HttpServletResponse response) {
        ExcelUtil<Jtlxpw> util = new ExcelUtil<Jtlxpw>(Jtlxpw.class);
        util.importTemplateExcel(response, "集团立项批文数据");
    }

    /**
     * 导入数据
     */
    @Log(title = "集团立项批文", businessType = BusinessType.IMPORT)
    @PreAuthorize("@ss.hasPermi('sbxm:jtlxpw:import')")
    @PostMapping("/importData")
    public AjaxResult importData(MultipartFile file) throws Exception {
        ExcelUtil<Jtlxpw> util = new ExcelUtil<Jtlxpw>(Jtlxpw.class);
        InputStream inputStream = file.getInputStream();
        List<Jtlxpw> list = util.importExcel(inputStream);
        inputStream.close();
        int count = jtlxpwService.batchInsertJtlxpw(list);
        return AjaxResult.success("导入成功" + count + "条信息！");
    }

    /**
     * 获取集团立项批文详细信息
     */
    @PreAuthorize("@ss.hasPermi('sbxm:jtlxpw:query')")
    @GetMapping(value = "/{jtlxpwId}")
    public AjaxResult getInfo(@PathVariable("jtlxpwId") String jtlxpwId) {
        return success(jtlxpwService.selectJtlxpwByJtlxpwId(jtlxpwId));
    }

    /**
     * 新增集团立项批文
     */
    @PreAuthorize("@ss.hasPermi('sbxm:jtlxpw:add')")
    @Log(title = "集团立项批文", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Jtlxpw jtlxpw) {
        jtlxpw.setUserId(Math.toIntExact(getUserId())); //用户ID
        jtlxpw.setDeptId(Math.toIntExact(getDeptId())); //部门ID
        jtlxpw.setCreateBy(getUsername()); //创建人
        return toAjax(jtlxpwService.insertJtlxpw(jtlxpw));
    }

    /**
     * 修改集团立项批文
     */
    @PreAuthorize("@ss.hasPermi('sbxm:jtlxpw:edit')")
    @Log(title = "集团立项批文", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Jtlxpw jtlxpw) {
        return toAjax(jtlxpwService.updateJtlxpw(jtlxpw));
    }

    /**
     * 删除集团立项批文
     */
    @PreAuthorize("@ss.hasPermi('sbxm:jtlxpw:remove')")
    @Log(title = "集团立项批文", businessType = BusinessType.DELETE)
    @DeleteMapping("/{jtlxpwIds}")
    public AjaxResult remove(@PathVariable String[] jtlxpwIds) {
        return toAjax(jtlxpwService.deleteJtlxpwByJtlxpwIds(jtlxpwIds));
    }

    /**
     * 获得集团立项批文主键ID (UUID)
     */
    @GetMapping("/getLxPwId")
    public AjaxResult getZxTbId(){
        String LxPw = IdUtils.fastSimpleUUID();
        return success(LxPw);
    }

    /**
     * 新增附件
     */
    @PostMapping("/add/File/By/subId")
    public AjaxResult insertJtlxpwFjByJtlxpwId(@RequestBody JtlxpwFj jtlxpwFj){
        return toAjax(jtlxpwService.insertJtlxpwFjByJtlxpwId(jtlxpwFj));
    }

    /**
     * 根据集团立项批文ID查询附件列表
     */
    @GetMapping("/get/FileList/By/subId")
    public AjaxResult selectJtlxpwFjList(String sId){
        JtlxpwFj jtlxpwFj = new JtlxpwFj();
        jtlxpwFj.setJtlxpwId(sId);
        return AjaxResult.success(jtlxpwService.selectJtlxpwFjList(jtlxpwFj));
    }

    /**
     * 删除集团立项批文附件
     */
    @Log(title = "集团立项批文附件", businessType = BusinessType.DELETE)
    @DeleteMapping("/fj/{fjIds}")
    public AjaxResult deleteJtlxpwFjByFjIds(@PathVariable Integer[] fjIds)
    {
        return toAjax(jtlxpwService.deleteJtlxpwFjByFjIds(fjIds));
    }

}
